######################################################################################################.
#Create two indices: freedom_restriction_index and power_concentration_index
######################################################################################################.


##########################################################################################################################
# create the two indices with standardized components (to avoid that scale has impact on additive index)
###########################################################################################################################


rm(list = ls())
data <- read_excel("Data/1_dataset.xlsx")


#replace 99 from non v-dem variables as missing
data$C4_gatherings[data$C4_gatherings==99] <- NA
data$C6_Stay_home[data$C6_Stay_home==99] <- NA
data$C7_internal_movement[data$C7_internal_movement==99] <- NA
data$C8_International_travel[data$C8_International_travel==99] <- NA
data$SoE[data$SoE==99] <- NA
data$FreeExpr[data$FreeExpr==99] <- NA

#Create variables and calculate indices

freedom <- c("C4_gatherings", "C6_Stay_home", "C7_internal_movement", "C8_International_travel") #all ordinal from low restrictions to high
public_sphere <- c("melim", "merepfact","merepgov", "merepoth") # all ordinal from low restrictions to high; 0 -3 de jure (melim), and 0 - 3 de facto (three dummies)


#reverse time_limit (i.e. no limit equals high democratic restrictions) and emlegapp (higher means less control by legislature)
data <- data %>% 
  mutate(emlimit_rev = (emlimit-1)*-1) # notimelit is high value (i.e. very restrictive measures)

data <- data %>% 
  mutate(emlegapp_rev = (emlegapp-1)*-1) # no parl control is high value (i.e. very restrictive measures)

power_concentration <- c("emlimit_rev", "emlegapp_rev", "leglimit", c(public_sphere))

all_measures <- c(c(freedom), c(public_sphere), c(power_concentration))

#standardize all variables
stand.fun <- function(x){
  x.out <- scale(x)
  return(x.out)
}

data_std <- data %>% 
  mutate_at(all_measures, stand.fun)

data_std$restrict_freedom_index <- rowSums(data_std[c(freedom)], na.rm = FALSE)
data_std$power_concentration_index <- rowSums(data_std[c(power_concentration)], na.rm = TRUE) #see above
data_std$power_concentration_index[is.na(data_std$leglimit)] <- NA

#recode backsliding from [-1,0] zu [0,1]
data_std <- data_std %>% 
  mutate(libdem10 = (libdem10*-1)) 


write.xlsx(data_std, "Data/2_data_final_std.xlsx")



##########################################################################################################################
# create separate power concentration and media control index
###########################################################################################################################


rm(list = ls())
data <- read_excel("Data/1_dataset.xlsx")


#replace 99 from non v-dem variables as missing
data$C4_gatherings[data$C4_gatherings==99] <- NA
data$C6_Stay_home[data$C6_Stay_home==99] <- NA
data$C7_internal_movement[data$C7_internal_movement==99] <- NA
data$C8_International_travel[data$C8_International_travel==99] <- NA
data$SoE[data$SoE==99] <- NA
data$FreeExpr[data$FreeExpr==99] <- NA

#Create variables and calculate indices

freedom <- c("C4_gatherings", "C6_Stay_home", "C7_internal_movement", "C8_International_travel") #all ordinal from low restrictions to high
public_sphere <- c("melim", "merepfact","merepgov", "merepoth") # all ordinal from low restrictions to high; 0 -3 de jure (melim), and 0 - 3 de facto (three dummies)


#reverse time_limit (i.e. no limit equals high democratic restrictions) and emlegapp (higher means less control by legislature)
data <- data %>% 
  mutate(emlimit_rev = (emlimit-1)*-1) # notimelit is high value (i.e. very restrictive measures)

data <- data %>% 
  mutate(emlegapp_rev = (emlegapp-1)*-1) # no parl control is high value (i.e. very restrictive measures)

power_concentration <- c("emlimit_rev", "emlegapp_rev", "leglimit")



all_measures <- c(c(freedom), c(public_sphere), c(power_concentration))

#standardize all variables
stand.fun <- function(x){
  x.out <- scale(x)
  return(x.out)
}

data_std <- data %>% 
  mutate_at(all_measures, stand.fun)

data_std$power_concentration_index <- rowSums(data_std[c(power_concentration)], na.rm = TRUE) #see above
data_std$power_concentration_index[is.na(data_std$leglimit)] <- NA

data_std$media_control_index <- rowSums(data_std[c(public_sphere)], na.rm = TRUE)
data_std$media_control_index[is.na(data_std$melim)] <- NA


#recode backsliding from [-1,0] zu [0,1]
data_std <- data_std %>% 
  mutate(libdem10 = (libdem10*-1)) 



write.xlsx(data_std, "Data/2_data_final_power_sep.xlsx")


##########################################################################################################################
# create indices for dataset with DB 2017 values
###########################################################################################################################
rm(list = ls())
data <- read_excel("Data/1_dataset_DB2017.xlsx")

#replace 99 from non v-dem variables as missing
data$C4_gatherings[data$C4_gatherings==99] <- NA
data$C6_Stay_home[data$C6_Stay_home==99] <- NA
data$C7_internal_movement[data$C7_internal_movement==99] <- NA
data$C8_International_travel[data$C8_International_travel==99] <- NA
data$SoE[data$SoE==99] <- NA
data$FreeExpr[data$FreeExpr==99] <- NA

#Create variables and calculate indices

freedom <- c("C4_gatherings", "C6_Stay_home", "C7_internal_movement", "C8_International_travel") #all ordinal from low restrictions to high
public_sphere <- c("melim", "merepfact","merepgov", "merepoth") # all ordinal from low restrictions to high; 0 -3 de jure (melim), and 0 - 3 de facto (three dummies)


#reverse time_limit (i.e. no limit equals high democratic restrictions) and emlegapp (higher means less control by legislature)
data <- data %>% 
  mutate(emlimit_rev = (emlimit-1)*-1) # notimelit is high value (i.e. very restrictive measures)

data <- data %>% 
  mutate(emlegapp_rev = (emlegapp-1)*-1) # no parl control is high value (i.e. very restrictive measures)

power_concentration <- c("emlimit_rev", "emlegapp_rev", "leglimit", c(public_sphere))


all_measures <- c(c(freedom), c(public_sphere), c(power_concentration))

#standardize all variables
stand.fun <- function(x){
  x.out <- scale(x)
  return(x.out)
}

data_std <- data %>% 
  mutate_at(all_measures, stand.fun)

data_std$restrict_freedom_index <- rowSums(data_std[c(freedom)], na.rm = FALSE)
data_std$power_concentration_index <- rowSums(data_std[c(power_concentration)], na.rm = TRUE) #see above
data_std$power_concentration_index[is.na(data_std$leglimit)] <- NA

#recode backsliding from [-1,0] zu [0,1]
data_std <- data_std %>% 
  mutate(libdem10 = (libdem10*-1)) 


write.xlsx(data_std, "Data/2_data_final_2017_std.xlsx")
